package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.server.service.IPortalService;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.form.PorletForm;
import com.sitech.portal.system.service.ISysRoleService;

public class PorletAction extends JGridBaseAction{
	private IPortalService portalService;
	private ISysRoleService sysRoleService;
	private PorletForm porletForm = new PorletForm();
	private String flag;
	private List<TblSYS_ROLE> roleList ;
	private Map porletMap;

	public ISysRoleService getSysRoleService() {
		return sysRoleService;
	}

	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public List<TblSYS_ROLE> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<TblSYS_ROLE> roleList) {
		this.roleList = roleList;
	}

	public Map getPorletMap() {
		return porletMap;
	}

	public void setPorletMap(Map porletMap) {
		this.porletMap = porletMap;
	}

	public IPortalService getPortalService() {
		return portalService;
	}

	public void setPortalService(IPortalService portalService) {
		this.portalService = portalService;
	}

	public PorletForm getPorletForm() {
		return porletForm;
	}

	public void setPorletForm(PorletForm porletForm) {
		this.porletForm = porletForm;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public String addPorlet(){
		String cmd = this.getRequest().getParameter("cmd");
		String porletId = this.getRequest().getParameter("porletId");
		if(porletId!=null&&porletId.length()>0){
			porletMap = portalService.findPorletById(porletId);
//	        if (porletMap != null) {
//	            String role_ids = (String)porletMap.get("ROLE_IDS");
//	            if(role_ids!=null){
//	            	porletMap.put("ROLE_ID", role_ids.split(","));
//	            }
//	        }
			porletForm.setPorletId(porletMap.get("PORLET_ID").toString());
			porletForm.setTitle(porletMap.get("TITLE").toString());
			porletForm.setHeight(porletMap.get("HEIGHT").toString());
			porletForm.setContentUrl(porletMap.get("CONTENT_URL").toString());
			porletForm.setMaxUrl(porletMap.get("MAX_URL")==null?"":porletMap.get("MAX_URL").toString());
			porletForm.setRoleIds(porletMap.get("ROLE_IDS").toString());
			porletForm.setDescription(porletMap.get("DESCRIPTION")==null?"":porletMap.get("DESCRIPTION").toString());
			porletForm.setSecond(porletMap.get("SECOND")==null?"":porletMap.get("SECOND").toString());
		}
		porletForm.setCmd(cmd);
        roleList = sysRoleService.findForListToAll();
        return SUCCESS;
	}

	public String saveOrUpdatePorlet() throws Exception {
		flag = "true";
        String[] ROLE_ID = this.getRequest().getParameterValues("multiselect_0");//谷歌浏览器
        if(ROLE_ID==null){
        	ROLE_ID = porletForm.getRoleIds().split(",");//ie8
        }
        Map porlet = new HashMap();
        porlet.put("PORLET_ID", porletForm.getPorletId());
        porlet.put("TITLE", porletForm.getTitle());
        porlet.put("HEIGHT", porletForm.getHeight());
        porlet.put("CONTENT_URL", porletForm.getContentUrl());
        porlet.put("MAX_URL", porletForm.getMaxUrl());
        porlet.put("ROLE_ID", ROLE_ID);
        porlet.put("DESCRIPTION", porletForm.getDescription());
        porlet.put("SECOND", porletForm.getSecond());
        try {
            if ("update".equalsIgnoreCase(porletForm.getCmd())) {
            	portalService.updatePORLET(porlet);
            } else {
            	Map map = portalService.findPorletById(porletForm.getPorletId());
            	if(map!=null)
            		throw new Exception("Panel ID已存在，请重新输入");
            	portalService.insertPORLET(porlet);
            }
            //更新Panel角色关系
            portalService.updateRoleMapOfPorlet(porletForm.getPorletId(),ROLE_ID);
        } catch (Exception ex) {
        	ex.printStackTrace();
            flag = ex.getMessage();
        }
        return SUCCESS;
    }

    public String deletePorlet() throws Exception {
    	flag = "true";
    	try {
	        String PORLET_ID = this.getRequest().getParameter("porletIds");
	        portalService.deletePORLETById(PORLET_ID);
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }

    public String porletList() throws Exception {
		return SUCCESS;
    }
    public String porletListQueryJson() throws Exception {
    	IPage result = portalService.findAllPorletsList(this.getPageForm());
    	
    	List list = (ArrayList)result.getRows();
    	for(int i=0;i<list.size();i++){
    		HashMap map = (HashMap)list.get(i);
    		String url = map.get("CONTENT_URL").toString();
    		
    		try{
	    		List itemList = (ArrayList)JSONUtil.fromJSON(url);
	    		
	    		String str = "";
	    		for(int j=0;j<itemList.size();j++){
	    			HashMap itemMap = (HashMap)itemList.get(j);
	    			str += itemMap.get("url")+"\r\n";
	    		}
	    		map.put("CONTENT_URL", str);
    		}catch(Exception e){
    			
    		}
    	}
    	this.setResult(result);

		return SUCCESS;
    }
    
}
